#include "DAC_CMD.hpp"

void DAC_CMD::printHelpTable()
{
    rt_kprintf("Usage: dac_cmd:\r\n");
    rt_kprintf("dac_cmd reset\r\n");
    rt_kprintf("dac_cmd init <address>\r\n");
    rt_kprintf("dac_cmd wakeup\r\n");
    rt_kprintf("dac_cmd soft_update\r\n");
    rt_kprintf("dac_cmd read_address\r\n");
    rt_kprintf("dac_cmd read\r\n");
    rt_kprintf("dac_cmd set_address <address>\r\n");
    rt_kprintf("dac_cmd set_ref <channel(A/B/C/D)> <refVol(VDD/VRef)>\r\n");
    rt_kprintf("dac_cmd set_gain <channel(A/B/C/D)> <gain(1/2)>\r\n");
    rt_kprintf("dac_cmd set_pd <channel(A/B/C/D)> <power_down(0/1/100/500)>\r\n");
    rt_kprintf("dac_cmd wr_dac <channel(A/B/C/D)> <vol(u16)>\r\n");
    rt_kprintf("dac_cmd wr_fast <vA(u16)> <vB(u16)> <vC(u16)> <vD(u16)>\r\n");
}

static int dac_cmd(int argc, char **argv)
{
    //* cmd table
    if (argc < 2)
    {
        DAC_CMD::printHelpTable();
        return 0;
    }

    string cmd = argv[1];
    auto cmdMail = ThreadCtrl::getInstance().cmdMail;
    static vector<string> cmdVec;
    cmdVec.clear();
    if (cmd == "reset")
    {
        cmdVec.push_back("reset");
        cmdMail->put(&cmdVec);
    }
    else if (cmd == "init")
    {
        cmdVec.push_back("init");
        cmdMail->put(&cmdVec);
    }
    else if (cmd == "wakeup")
    {
        cmdVec.push_back("wakeup");
        cmdMail->put(&cmdVec);
    }
    else if (cmd == "soft_update")
    {
        cmdVec.push_back("soft_update");
        cmdMail->put(&cmdVec);
    }
    else if (cmd == "read_address" || cmd == "read_addr")
    {
        cmdVec.push_back("read_address");
        cmdMail->put(&cmdVec);
    }
    else if (cmd == "read")
    {
        cmdVec.push_back("read");
        cmdMail->put(&cmdVec);
    }
    else if (cmd == "set_address" || cmd == "set_addr")
    {
        if (argc < 3)
        {
            DAC_CMD::printHelpTable();
            return 0;
        }
        cmdVec.push_back("set_address");
        string address = argv[2];
        cmdVec.push_back(address);
        cmdMail->put(&cmdVec);
    }
    else if (cmd == "set_ref")
    {
        if (argc < 4)
        {
            DAC_CMD::printHelpTable();
            return 0;
        }
        cmdVec.push_back("set_ref");
        string channel = argv[2];
        cmdVec.push_back(channel);
        string vol = argv[3];
        cmdVec.push_back(vol);
        cmdMail->put(&cmdVec);
    }
    else if (cmd == "set_gain")
    {
        if (argc < 4)
        {
            DAC_CMD::printHelpTable();
            return 0;
        }
        cmdVec.push_back("set_gain");
        string channel = argv[2];
        cmdVec.push_back(channel);
        string gain = argv[3];
        cmdVec.push_back(gain);
        cmdMail->put(&cmdVec);
    }
    else if (cmd == "set_pd")
    {
        if (argc < 4)
        {
            DAC_CMD::printHelpTable();
            return 0;
        }
        cmdVec.push_back("set_pd");
        string channel = argv[2];
        cmdVec.push_back(channel);
        string power_down = argv[3];
        cmdVec.push_back(power_down);
        cmdMail->put(&cmdVec);
    }
    else if (cmd == "wr_dac" || cmd == "write_dac")
    {
        if (argc < 4)
        {
            DAC_CMD::printHelpTable();
            return 0;
        }
        cmdVec.push_back("wr_dac");
        string channel = argv[2];
        cmdVec.push_back(channel);
        string data = argv[3];
        cmdVec.push_back(data);
        cmdMail->put(&cmdVec);
    }
    else if (cmd == "wr_fast" || cmd == "write_fast")
    {
        if (argc < 6)
        {
            DAC_CMD::printHelpTable();
            return 0;
        }
        cmdVec.push_back("wr_fast");
        string valueA = argv[2];
        cmdVec.push_back(valueA);
        string valueB = argv[3];
        cmdVec.push_back(valueB);
        string valueC = argv[4];
        cmdVec.push_back(valueC);
        string valueD = argv[5];
        cmdVec.push_back(valueD);
        cmdMail->put(&cmdVec);
    }
    else
    {
        DAC_CMD::printHelpTable();
    }
    rt_thread_mdelay(100);
    return 0;
}

MSH_CMD_EXPORT(dac_cmd, DAC cmd);
